﻿using System;
using LTP.Accounts.Bus;
using System.Web.Security;

using ChinaSoft.Model;
using ChinaSoft.BLL;
using System.Collections.Generic;

namespace Maticsoft.Web.Admin
{
    /// <summary>
    /// Login 的摘要说明。
    /// </summary>
    public partial class Login : System.Web.UI.Page
    {

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ViewState["GUID"] = System.Guid.NewGuid().ToString();
                this.lblGUID.Text = this.ViewState["GUID"].ToString();
            }
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnLogin.Click += new System.Web.UI.ImageClickEventHandler(this.btnLogin_Click);

        }
        #endregion

        private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
            {
                int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
                if (PassErroeCount > 3)
                {
                    txtUsername.Disabled = true;
                    txtPass.Disabled = true;
                    btnLogin.Enabled = false;
                    this.lblMsg.Text = "对不起，你错误登录了三次，系统登录锁定！";
                    return;
                }

            }

            #region 检查验证码
            if ((Session["CheckCode"] != null) && (Session["CheckCode"].ToString() != ""))
            {
                if (Session["CheckCode"].ToString().ToLower() != this.CheckCode.Value.ToLower())
                {
                    this.lblMsg.Text = "所填写的验证码与所给的不符 !";
                    Session["CheckCode"] = null;
                    return;
                }
                else
                {
                    Session["CheckCode"] = null;
                }
            }
            else
            {
                Response.Redirect("login.aspx");
            }
            #endregion

            string userAccount = Maticsoft.Common.PageValidate.InputText(txtAccount.Value.Trim(), 30);
            string userName = Maticsoft.Common.PageValidate.InputText(txtUsername.Value.Trim(), 30);
            string Password = Maticsoft.Common.PageValidate.InputText(txtPass.Value.Trim(), 30);
            //加密密码
            Password = Maticsoft.Common.DEncrypt.DEncrypt.MD5(Password, 16);
            //验证登录信息，如果验证通过则返回当前用户对象的安全上下文信息
            BLL_tb_MidSoft_Users userMgr = new BLL_tb_MidSoft_Users();
            List<Model_tb_MidSoft_Users> user= userMgr.GetModelList("UserAccount='" + Server.HtmlEncode(userAccount) + "'");
            //Md5+盐值加密算法，相对安全
            if (user != null &&user.Count>0&& user[0].UserPassword.Trim() == Password)//CJ_DBOperater.CJ.PwdSecurity(Password)
            {
                if (user[0].State == 1)
                {
                    this.lblMsg.Text = "登陆失败： " + userName + " 该账号已经被禁用!";
                    Session["PassErrorCountAdmin"] = Convert.ToInt16(Session["PassErrorCountAdmin"]) + 1;
                }
                else
                {
                    Session["currentUserID"] = user[0].UsersId;
                    Session["UserName"] = user[0].UserName;
                    Session["UserAccount"] = user[0].UserAccount;
                    Session["usershowname"] = user[0].UserName;
                    Session["UserDepartMent"] = "信息中心";
                    Session["UserDepartMentUse"] = "信息中心";
                    Session["userpwd"] = user[0].UserPassword.Trim();
                    BLL_tb_MidSoft_UserHaveRoles userHaveRolesMgr = new BLL_tb_MidSoft_UserHaveRoles();
                    List<Model_tb_MidSoft_UserHaveRoles> userHaveRoles = userHaveRolesMgr.GetModelList("UsersId=" + user[0].UsersId);

                    if (userHaveRoles != null && userHaveRoles.Count > 0)
                        Session["role"] = userHaveRoles[0].RolesId.ToString().Trim();

                    #region 登陆日志
                    BLL_tb_MidSoft_log logMgr = new BLL_tb_MidSoft_log();
                    Model_tb_MidSoft_log log = new Model_tb_MidSoft_log();
                    log.CreateTime = DateTime.Now;
                    log.description = "登陆日志";
                    log.userAccount = user[0].UserAccount;
                    log.userName = user[0].UserName;
                    logMgr.Add(log);//记录用户当前登录时间
                    #endregion
                    Session["PassErrorCountAdmin"] = 0;
                    Response.Redirect("Systems/Main.aspx");
                }
            }
            else
            {
                this.lblMsg.Text = "登陆失败： " + userName + " 密码不正确!";
                Session["PassErrorCountAdmin"] = Convert.ToInt16(Session["PassErrorCountAdmin"]) + 1;
            }
        }





    }
}
